package src 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	/**
	 * ...
	 * @author Morozov
	 * 
	 */
	public class Main extends MovieClip
	{
		private var currentState:String = "start";
		
		private var gameEngine:Engine;
		
		public function Main() 
		{
			switchGameState("preloader");
		}
		/**
		 * Changes current game state
		 * @param	newState "preloader", "menu" or "game"
		 */
		private function switchGameState(newState:String = "game"):void 
		{
			if (newState == currentState)
			{
				return;
			}
			switch(currentState)
			{
				case "preloader" : 
					this.removeEventListener(Event.ENTER_FRAME, startPreloader);
					break;
					
				case "menu" : 
					
					break;
					
				case "game" :
					
					break;
			}
			switch(newState)
			{
				case "preloader": 
					this.addEventListener(Event.ENTER_FRAME, startPreloader);
					gotoAndStop(1, "Scene 1");
					break;
				case "menu":
					gotoAndStop(2, "Scene 1");
					break;
				case "game":
					gameEngine = new Engine();
					gotoAndStop(1, "Scene 2");
					this.addChild(gameEngine);
					break;
			}
			currentState = newState;
		}
		
		private function startPreloader(e:Event):void 
		{
			var bytesLoaded:Number = stage.loaderInfo.bytesLoaded;
			var bytesTotal:Number = stage.loaderInfo.bytesTotal;
			
			trace(bytesLoaded, "/", bytesTotal);
			
			if (bytesTotal > 0)
			{
				//TODO: add a progress bar	
				//this.progressBar.gotoAndStop(bytesTotal / ( bytesTotal * 0.02));
			}			
			if (bytesLoaded == bytesTotal || bytesTotal == 0)
			{
				switchGameState("game");
			} 
		}
	
	}

}